//
//  RadioViewController.m
//  CwinCar
//
//  Created by dlios on 15/10/21.
//  Copyright © 2015年 dlios. All rights reserved.
//

#import "RadioViewController.h"

@interface RadioViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@end

@implementation RadioViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.title = @"电台";
    [self parseData];
    self.view.backgroundColor = [UIColor whiteColor];
    
}

#pragma mark- 解析数据
-(void)parseData
{
    [AFNblock getdataWithString:@"http://app.api.autohome.com.cn/autov4.9.5/News/newsmyradiolist-pm2-tp0.json" block:^(id block) {
        
        [self creatCollect];
        self.radioArray = [[NSMutableArray alloc]init];
        for (NSDictionary *dic in [[block objectForKey:@"result"]objectForKey:@"list"]) {
            RadioModel *model = [RadioModel modelWithDictionary:dic];
            [self.radioArray addObject:model];
        }
    }];
        
}
#pragma mark- 创建collectionView
-(void)creatCollect
{
    _flow = [[UICollectionViewFlowLayout alloc]init];
    
    _flow.itemSize = CGSizeMake(mainScreen_Width*2./5., 200);
    
    _flow.minimumInteritemSpacing = 1;
    
    _flow.minimumLineSpacing = 15;
    
    _flow.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    
    
    //集合视图
    _collectV = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, mainScreen_Width, mainScree_Height-64) collectionViewLayout:_flow];
    _collectV.backgroundColor = [UIColor whiteColor];
    _collectV.delegate = self;
    _collectV.dataSource = self;
    
    [self.view addSubview:_collectV];
    [_collectV registerClass:[DetailCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.radioArray.count;
}
//cell
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    RadioModel *model = [self.radioArray objectAtIndex:indexPath.item];
    [cell setRadioModel:model];
    cell.rangeLabel.hidden = YES;
    if (cell.isType == YES) {
        cell.rangeLabel.hidden = NO;
    }
    return cell;
}
//点击方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    RadioDetailViewController *inside = [[RadioDetailViewController alloc]init];
    RadioModel *model = [self.radioArray objectAtIndex:indexPath.item];
    inside.model = model;
    
    [self.navigationController pushViewController:inside animated:NO];
    

    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
